package seTOee.annotation.Demo2;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;
import java.util.Arrays;
/**
 * 解析注释
 * */
public class AnnotationTest {
    /**
     * 解析类上的注解
     * 1、获取当前类的Class对象
     * 2、判断当前类是否被指定的注解所标注
     * 3、获取注解对象
     * 4、获取注解的属性值
     * */
    @Test
    public void test1() {
        //1、获取当前类的Class对象
        Class<Demo> c = Demo.class;
        //2、判断是否有对应的注释
        if (c.isAnnotationPresent(MyTest.class)) {
            //3、获取注解对象
            MyTest annotation = c.getDeclaredAnnotation(MyTest.class);
            //4、获取注解的属性值
            System.out.println(annotation.value());
            System.out.println(annotation.height());
            String[] address = annotation.address();
            System.out.println(Arrays.toString(address));

        }
    }

    /**
     * 解析方法上的注解
     * 1、获取当前类的Class对象
     * 2、获取方法对象
     * 3、判断当前方法上是否有指定的注解
     * 4、获取注解对象
     * 5、获取注解的属性值
     * */
    @Test
    public void test2() throws Exception {
        Class<Demo> c = Demo.class;
        Method go = c.getDeclaredMethod("go");
        if(go.isAnnotationPresent(MyTest.class)){
            MyTest annotation = go.getDeclaredAnnotation(MyTest.class);
            System.out.println(annotation.value());
            System.out.println(annotation.height());
            String[] address = annotation.address();
            System.out.println(Arrays.toString(address));
        }
    }

}
